//
//  BaseViewController.m
//  YYBG_IOS
//
//  Created by wyl on 2017/2/22.
//  Copyright © 2017年 wyl. All rights reserved.
//

#import "BaseViewController.h"


const static int btnWidth = 38 ;

@interface BaseViewController ()<UINavigationControllerDelegate>

@end

@implementation BaseViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    self.edgesForExtendedLayout = UIRectEdgeNone ;
  
    //无网络
    _noDateView = [[BaseNoDate alloc]initWithFrame:CGRectMake(0, 0, APPWIDTH, APPHEIGHT)];
    [_noDateView setTypeAndTitle:NoContentTypeNetwork title:@"没有网络"];
    _noDateView.hidden= YES ;
    UITapGestureRecognizer*tapGesture = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(reflashDate)];
    [_noDateView addGestureRecognizer:tapGesture];
    [self.view addSubview:_noDateView];
    
    self.navigationController.delegate = self;
    
}
-(void)reflashDate{
    
}
//初始化
-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
 
    //标题栏不透明
    self.navigationController.navigationBar.translucent = NO;
    //底部导航栏不透明
    self.tabBarController.tabBar.translucent = NO ;
     //标题
    self.navigationItem.title = @"";
    self.navigationItem.titleView = nil;
    
    //标题背景颜色，默认白色
    self.navigationController.navigationBar.barTintColor  = [UIColor whiteColor];
    
    self.navigationItem.leftBarButtonItem = nil ;
    
    self.navigationItem.hidesBackButton = YES ;
    
    __weak typeof(self) weakSelf = self;
    self.navigationController.interactivePopGestureRecognizer.delegate = weakSelf;
     //时间颜色
    [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleDefault animated:NO];
    //设置标题栏文字颜色
    NSDictionary * dict = [NSDictionary dictionaryWithObject:[UIColor blackColor] forKey:UITextAttributeTextColor];
    
    //标题栏文字颜色
    [self setTopBarTitleColor:[UIColor whiteColor]];
    
    [self topLeftItemButton:nil title:nil];
    
    [self topRightItemButton:nil image2:nil title1:nil title2:nil];
}

-(void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    self.navigationController.navigationBar.translucent = NO;
    self.edgesForExtendedLayout = UIRectEdgeNone ;
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    
}

//顶部左边按钮
-(void)topLeftItemButton:(UIImage *)image title:(NSString *)title{
    NSInteger textFont = 12 ;
    //文字颜色
    UIColor *textColor = [UIColor colorWithHexString:COLOR_DARK_GRAY];
    //按钮大小
    CGSize btnSize =  CGSizeMake(btnWidth, btnWidth) ;
    
    UIView *leftBtnView = [[UIView alloc] initWithFrame:CGRectMake(0, 0 , btnWidth, btnWidth)];
    if (image) {
        //文字大小
      
        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
        [button setImage:image forState:UIControlStateNormal];
        button.titleLabel.systemFont = textFont ;
        button.size = btnSize;//
        button.x = -8 ;
        button.tag = 103 ;
        if (title) {
            [button setTitle:title forState:UIControlStateNormal];
            [button setTitleColor:textColor forState:UIControlStateNormal];
            [button layoutButtonWithEdgeInsetsStyle:MKButtonEdgeInsetsStyleTop imageTitleSpace:3];
        }
        
        [leftBtnView addSubview:button];
        [button addTarget:self action:@selector(leftAction:) forControlEvents:UIControlEventTouchUpInside];
        
        UIBarButtonItem *backNavigationItem = [[UIBarButtonItem alloc] initWithCustomView:leftBtnView];
        self.navigationItem.leftBarButtonItem = backNavigationItem ;
    }else if(title){
  
        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
        button.titleLabel.systemFont = 14 ;
        button.size = btnSize;
        [button setTitle:title forState:UIControlStateNormal];
        [button setTitleColor:textColor forState:UIControlStateNormal];
        [leftBtnView addSubview:button];
        
        button.x = -8 ;
        button.tag = 103 ;
        [button addTarget:self action:@selector(leftAction:) forControlEvents:UIControlEventTouchUpInside];
        [leftBtnView addSubview:button];
        UIBarButtonItem* leftItem = [[UIBarButtonItem alloc]initWithCustomView:leftBtnView];
        self.navigationItem.leftBarButtonItem= leftItem;
    }else {
        self.navigationItem.leftBarButtonItem = nil ;
    }
}

//顶部右边按钮
-(void)topRightItemButton:(UIImage *)image1 image2:(UIImage *)image2 title1:(NSString *)title1 title2:(NSString *)title2{
    //修改方法
    UIView *rightBtnView = [[UIView alloc] initWithFrame:CGRectMake(0,0, btnWidth, btnWidth)];
     //文字大小
    NSInteger textFont = 12 ;
    //文字颜色
    UIColor *textColor = [UIColor colorWithHexString:COLOR_DARK_GRAY];
    //按钮大小
    CGSize btnSize =  CGSizeMake(btnWidth, btnWidth) ;
    if (image1) {
        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
        [button setImage:image1 forState:UIControlStateNormal];
        button.titleLabel.systemFont = textFont ;
        button.size = btnSize;
        button.tag = 1001 ;
        if (title1) {
            [button setTitle:title1 forState:UIControlStateNormal];
            [button setTitleColor:textColor forState:UIControlStateNormal];
            [button layoutButtonWithEdgeInsetsStyle:MKButtonEdgeInsetsStyleTop imageTitleSpace:3];
        }
        [rightBtnView addSubview:button];
        button.x = 8 ;
        [button addTarget:self action:@selector(right1Action:) forControlEvents:UIControlEventTouchUpInside];
    }else if(title1){
        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
        button.titleLabel.systemFont = 14 ;
        button.size = btnSize;
        [button setTitle:title1 forState:UIControlStateNormal];
        [button setTitleColor:textColor forState:UIControlStateNormal];
        [rightBtnView addSubview:button];
        button.x = 8 ;
        button.tag = 1001 ;
        [button addTarget:self action:@selector(right1Action:) forControlEvents:UIControlEventTouchUpInside];
    }
    if (image2) {
        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
        [button setImage:image2 forState:UIControlStateNormal];
        button.titleLabel.systemFont = textFont ;
        button.size = btnSize;
        if (title2) {
            [button setTitle:title2 forState:UIControlStateNormal];
            [button setTitleColor:textColor forState:UIControlStateNormal];
            [button layoutButtonWithEdgeInsetsStyle:MKButtonEdgeInsetsStyleTop imageTitleSpace:3];
        }
        rightBtnView.width = 82 ;
        button.x = btnWidth + 8 + 8;
         button.tag = 1002 ;
        [rightBtnView addSubview:button];
        [button addTarget:self action:@selector(right2Action:) forControlEvents:UIControlEventTouchUpInside];
    }else if(title2){
        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
        button.titleLabel.systemFont = 14 ;
        button.size = btnSize;
        [button setTitle:title2 forState:UIControlStateNormal];
        [button setTitleColor:textColor forState:UIControlStateNormal];
        rightBtnView.width = 82 ;
        button.x = btnWidth + 8 + 8;
        button.tag = 1002 ;
        [rightBtnView addSubview:button];
        [button addTarget:self action:@selector(right2Action:) forControlEvents:UIControlEventTouchUpInside];
    }
    UIBarButtonItem* rightItem = [[UIBarButtonItem alloc]initWithCustomView:rightBtnView];
    self.navigationItem.rightBarButtonItem= rightItem;
}
-(UIButton *)getLeftButton {
    UIBarButtonItem* leftItem = self.navigationItem.leftBarButtonItem ;
    if (leftItem) {
        UIView *leftView  = leftItem.customView ;
        if (leftView) {
            UIButton *btn = [leftView viewWithTag:103 ];
            return btn ;
        }
    }
    return nil ;
}
-(UIButton *)getRightButton1 {
    UIBarButtonItem* rightItem = self.navigationItem.rightBarButtonItem ;
    if (rightItem) {
        UIView *rightView  = rightItem.customView ;
        if (rightView) {
            UIButton *btn = [rightView viewWithTag:1001 ];
            return btn ;
        }
    }
    return nil ;
}
-(UIButton *)getRightButton2 {
    UIBarButtonItem* rightItem = self.navigationItem.rightBarButtonItem ;
    if (rightItem) {
        UIView *rightView  = rightItem.customView ;
        if (rightView) {
            UIButton *btn = [rightView viewWithTag:1002 ];
            return btn ;
        }
    }
    return nil ;
}

//设置标题
-(void)setTopBarTitle:(NSString *)title{
    UILabel *titleLabel = [[UILabel alloc]init];
    titleLabel.text = title ;
    [titleLabel sizeToFit];
    self.navigationItem.title = title;
    self.tabBarController.navigationItem.titleView = titleLabel ;
}
//设置背景栏颜色
-(void)setTopBarBackgroud:(UIColor *)color{
    self.navigationController.navigationBar.barTintColor = color;
    self.tabBarController.navigationController.navigationBar.barTintColor = color;
}
//设置时间栏颜色（黑色或者白色）
-(void)setStateBackgroud:(BOOL)black{
    if (black) {
         [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleDefault animated:NO];
    }else {
         [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent animated:NO];
    }
}
-(void)setTopBarTitleColor:(UIColor *)color{
    if (self.tabBarController.navigationItem.titleView) {
        if ([self.tabBarController.navigationItem.titleView isKindOfClass:UILabel.class]) {
            UILabel *label  = (UILabel *)self.navigationItem.titleView ;
            label.textColor = color ;
        }
    }
}
//左边按钮
-(void)leftAction:(UIButton *)button{
    NSLog(@"leftAction");
}
//右边第一个按钮
-(void)right1Action:(UIButton *)button{
    NSLog(@"right1Action");
}
//右边第二个按钮
-(void)right2Action:(UIButton *)button{
    NSLog(@"right2Action");
}
//隐藏导航栏
-(void)hideNavigationBarHidden{
    [self.tabBarController.navigationController setNavigationBarHidden:YES animated:NO];//这两行代码
    [self.navigationController setNavigationBarHidden:YES animated:NO];
}
-(void)hideTabBar{
//   self.tabBarController.tabBar.hidden = YES;
}
//设置导航栏为透明状态
-(void)transparentNavigationBar {
    self.edgesForExtendedLayout = UIRectEdgeAll ;
    self.navigationController.navigationBar.translucent = YES;
    self.navigationController.navigationBar.shadowImage = [[UIImage alloc] init];
    UIImage *image = [self createAImageWithColor:[UIColor clearColor] alpha:0.0];
    [self.navigationController.navigationBar setBackgroundImage:image forBarMetrics:UIBarMetricsDefault];
}

- (UIImage *)createAImageWithColor:(UIColor *)color alpha:(CGFloat)alpha{
    CGRect rect = CGRectMake(0.0f, 0.0f, 1.0f, 1.0f);
    UIGraphicsBeginImageContext(rect.size);
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSetFillColorWithColor(context, [color CGColor]);
    CGContextSetAlpha(context, alpha);
    CGContextFillRect(context, rect);
    UIImage *theImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return theImage;
}

-(void)showNoNetWortOrDate {
    self.noDateView.hidden = NO ;
    [self.noDateView bringSubviewToFront:self.view];
    
}
@end
